package com.github.lee.mvvm.core.ui

import android.graphics.Color
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.github.lee.mvvm.core.databinding.LayoutBashRefreshBinding
import com.github.lee.mvvm.core.vm.BaseRefreshViewModel
import kotlin.random.Random
abstract class BaseRefreshFragment<VM : BaseRefreshViewModel<*,*>> :
    BaseFragment<LayoutBashRefreshBinding, VM>() {

    protected val mAdapter by lazy { pageVM.contentAdapter }
    protected var autoRefresh = true

    protected var enableRefresh = true
        set(value) {
            pageVB?.apply {
                if (value) {
                    srlRefreshRefresh.setOnRefreshListener {
                        onRefreshOrLoadMore(true)
                    }
                } else {
                    srlRefreshRefresh.setOnRefreshListener(null)
                }
                srlRefreshRefresh.isEnabled = value
            }
            field = value
        }
    protected var enableLoadMore = true
        set(value) {
            if (mAdapter is LoadMoreModule) {
                if (value) {
                    mAdapter.loadMoreModule.setOnLoadMoreListener(null)
                } else {
                    mAdapter.loadMoreModule.setOnLoadMoreListener {
                        onRefreshOrLoadMore(false)
                    }
                }
                mAdapter.loadMoreModule.isEnableLoadMore = value
            }
            field = value
        }


    override fun onCreateSuccess(): LayoutBashRefreshBinding =
        LayoutBashRefreshBinding.inflate(layoutInflater)


    override fun onObserveData() {
        super.onObserveData()
        pageVM.apply {
            refreshLiveData.observe(viewLifecycleOwner) {
                pageVB?.srlRefreshRefresh?.isRefreshing = it
                fun randomColor(): Int {
                    val random = Random(System.currentTimeMillis())
                    val r = random.nextInt(50, 150)
                    val g = random.nextInt(50, 150)
                    val b = random.nextInt(50, 150)
                    return Color.rgb(r, g, b)
                }

                pageVB?.srlRefreshRefresh?.setColorSchemeColors(
                    randomColor(), randomColor(), randomColor(), randomColor()
                )
            }
        }
    }

    override fun onSetViewListener() {
        super.onSetViewListener()
        pageVB?.apply {
            srlRefreshRefresh.setOnRefreshListener {
                onRefreshOrLoadMore(true)
            }
            if (mAdapter is LoadMoreModule) {
                mAdapter.loadMoreModule.setOnLoadMoreListener {
                    onRefreshOrLoadMore(false)
                }
            }
        }
    }

    override fun onSetViewData() {
        super.onSetViewData()
        pageVB?.apply {
            val lm = onCreateLayoutManager()
            rvRefreshContent.layoutManager = lm ?: LinearLayoutManager(mContext)
            rvRefreshContent.adapter = mAdapter
        }
        if (autoRefresh) {
            onRefreshOrLoadMore(true)
        }
    }

    //===Desc:=========================================================
    protected open fun onCreateLayoutManager(): RecyclerView.LayoutManager? = null
    protected open fun onRefreshOrLoadMore(isRefresh: Boolean = true) {

    }

    //===Desc:=========================================================

    protected fun addHeader(view: View, index: Int = -1, orientation: Int = LinearLayout.VERTICAL) {
        mAdapter.addHeaderView(view, index, orientation)
    }

    protected fun removeHeader(view: View) {
        mAdapter.removeHeaderView(view)
    }

    protected fun removeALlHeader() {
        mAdapter.removeAllHeaderView()
    }

    protected fun addFooter(view: View, index: Int = -1, orientation: Int = LinearLayout.VERTICAL) {
        mAdapter.addFooterView(view, index, orientation)
    }

    protected fun removeFooter(view: View) {
        mAdapter.removeFooterView(view)
    }

    protected fun removeALlFooter() {
        mAdapter.removeAllFooterView()
    }

    //===Desc:=========================================================

    protected fun getRecyclerView(): RecyclerView {
        return pageVB?.rvRefreshContent!!
    }

}